<template>
    <view class="page">
        <!-- 背景图片 -->
        <image
          src="/static/bj-001.jpg"
          mode="cover"
          class="bg-image"
        ></image>

        <view class="content-wrapper">
            <view v-if="iview" class="upload-bm" @click="handleIndex">
                <image src="/static/images/yunduo.png" class="yunduo-image"></image>
                <text class="signup-text">立即报名</text>
            </view>
            <view class="upload-bm query-button" @click="handleIndexQuery">
                <text class="signup-text1">查询结果</text>
              <image
                  class="action-image" style="width: 500rpx;height: 40rpx;"
                  mode="widthFix"
                  src="/static/images/query.png"
              ></image>
            </view>
        </view>
    </view>
</template>

<script>
import { wxLogin } from "@/api/login"
import { setToken } from '@/utils/auth'
import {getProActivity} from "@/api/activity/activity";
export default {
  data() {
    return {
      iview: false,
    };
  },
  onLoad() {
    // 页面加载时触发静默登录逻辑
    this.silentLogin();
    getProActivity().then(res => {
      if (res.code === 200) {
        if (res.data && res.data.id){
          this.iview = true;
        }
      }

    })
  },
  methods: {
    handleIndex() {
      this.$tab.reLaunch('/pages/syRule').catch(err => {
        console.error('跳转失败:', err);
        uni.showToast({ title: '跳转失败', icon: 'none' });
      });
    },
    handleIndexQuery() {
      this.$tab.reLaunch('/pages/saishi/queryCj').catch(err => {
        console.error('跳转失败:', err);
        uni.showToast({ title: '跳转失败', icon: 'none' });
      });
    },
    silentLogin() {
      // 检查是否在微信环境
      const isWechat = navigator.userAgent.toLowerCase().includes('micromessenger');
      if (!isWechat) {
        uni.showToast({ title: '请在微信中打开', icon: 'none' });
        return;
      }
      // 从URL中获取code
      const urlParams = new URLSearchParams(window.location.search);
      const code = urlParams.get('code');
      if (code) {
        // 发送code到后台
        this.sendCodeToBackend(code);
      } else {
        // 未获取到code，跳转微信授权页
        this.redirectToWechatAuth();
      }

    },
    redirectToWechatAuth() {
      const appId = 'wxd18f47635eaea06c';
      const redirectUri = encodeURIComponent('http://activity.hsmy.art/ss-h5/#/pages/login'); // 确保redirectUri正确编码
      const scope = 'snsapi_base'; // 静默授权
      const state = 'SILENT_LOGIN'; // 自定义状态标识
      const authUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirectUri}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`;
      window.location.href = authUrl;
    },
    async sendCodeToBackend(code) {
      try {
        const res = await wxLogin(code);
        if (res.code === 200) {
          // 登录成功，保存token或openid
          setToken(res.token);
          uni.redirectTo({ url: '/pages/home' });
        } else {
          uni.showToast({ title: '请联系管理员', icon: 'none' });
        }
      } catch (err) {
        uni.showToast({ title: '网络错误', icon: 'none' });
      }
    }
  }
}
</script>

<style lang="scss">
@font-face {
  font-family: 'minikt';
  src: url('@/static/font/minikt.TTF') format('truetype');
}
  .page {
    position: relative;
    width: 100%;
    height: 100vh;
  }

  .bg-image {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
  }

  .content-wrapper {
    position: absolute;
    top: 55%;
    left: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    flex-direction: column;
    align-items: center;
  }

  .upload-bm {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 40rpx;
    position: relative;
  }

  .query-button {
    position: relative;
    top: auto;
    left: auto;
    transform: none;
  }

  .signup-text {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 3;
    color: #004AAC;
    font-size: 60rpx;
    font-weight: bold;
    font-family: 'minikt', sans-serif;
    text-align: center;
    display: block;
    border-radius: 10px;
  }
  .signup-text1 {
    position: absolute;
    top: 50%;
    left: 55%;
    transform: translate(-50%, -50%);
    z-index: 3;
    color: #004AAC;
    font-size: 50rpx;
    font-weight: bold;
    font-family: 'minikt', sans-serif;
    text-align: center;
    display: block;
    border-radius: 10px;
  }
  .action-image {
    position: relative;
    z-index: 1;
  }
  .yunduo-image {
    position: relative;
    z-index: 1;
    width: 520rpx;
    height: 350rpx;
  }
</style>